﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AForge.Imaging.Filters;

namespace QuickPhotoFilter
{
    /// <summary>
    /// Логика взаимодействия для FilterSettingsWindow.xaml
    /// </summary>
    public partial class FilterSettingsWindow : Window
    {
        WriteableBitmap writeableBitmap;
        byte[] pixels;
        System.Drawing.Bitmap picture;
        BaseFilter baseFilter;
        Convolution convolutionFilter;
        BaseUsingCopyPartialFilter baseUsingCopyPartialFilter;
        BaseInPlacePartialFilter baseInPlacePartialFilter;

        public FilterSettingsWindow()
        {
            InitializeComponent();
            pixels = new byte[(int)(image.Width * image.Height * 4)];
        }

        public WriteableBitmap WriteableBitmap
        {
            get { return writeableBitmap; }
            set 
            { 
                writeableBitmap = QPF.ResizeWriteableBitmap(value, (int)image.Width, (int)image.Height);
                image.Source = writeableBitmap;
                writeableBitmap.CopyPixels(pixels, (int)writeableBitmap.Width * 4, 0);
            }
        }

        private void filterBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            System.Drawing.Imaging.PixelFormat pixelFormat = new System.Drawing.Imaging.PixelFormat();
            baseFilter = null;
            convolutionFilter = null;
            baseUsingCopyPartialFilter = null;
            baseInPlacePartialFilter = null;
            switch (filterBox.SelectedIndex)
            {
                case 0:
                    pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppRgb;
                    convolutionFilter = new Blur();
                    break;
                case 1:
                    pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppRgb;
                    baseUsingCopyPartialFilter = new Jitter(7);
                    break;
                case 2:
                    pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppRgb;
                    baseUsingCopyPartialFilter = new OilPainting(15);
                    break;
                case 3:
                    pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppRgb;
                    baseInPlacePartialFilter = new Sepia();
                    break;
                case 4:
                    pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppRgb;
                    baseFilter = new WaterWave()
                    {
                        HorizontalWavesCount = 10,
                        HorizontalWavesAmplitude = 5,
                        VerticalWavesCount = 3,
                        VerticalWavesAmplitude = 15
                    };
                    break;
            }
            picture = new System.Drawing.Bitmap((int)writeableBitmap.Width, (int)writeableBitmap.Height, pixelFormat);
            for(int i = 0; i < writeableBitmap.Width; ++i)
                for (int j = 0; j < writeableBitmap.Height; ++j)
                {
                    Color currentPixelColor = QPF.DetermineColor(new Point(i, j), pixels, (int)writeableBitmap.Width);
                    System.Drawing.Color color = System.Drawing.Color.FromArgb(currentPixelColor.A, currentPixelColor.R, currentPixelColor.G, currentPixelColor.B);
                    picture.SetPixel(i, j, color);
                }
            if (baseFilter != null)
                baseFilter.Apply(picture);
            if (convolutionFilter != null)
                convolutionFilter.ApplyInPlace(picture);
            if (baseUsingCopyPartialFilter != null)
                baseUsingCopyPartialFilter.ApplyInPlace(picture);
            if (baseInPlacePartialFilter != null)
                baseInPlacePartialFilter.ApplyInPlace(picture);

            for (int i = 0; i < picture.Width; ++i)
                for (int j = 0; j < picture.Height; ++j)
                {
                    System.Drawing.Color currentPixelColor = picture.GetPixel(i, j);
                    int firstByteNumber = QPF.PixelNumber(new Point(i, j), writeableBitmap.PixelWidth) * 4;
                    pixels[firstByteNumber] = currentPixelColor.B;
                    pixels[firstByteNumber + 1] = currentPixelColor.G;
                    pixels[firstByteNumber + 2] = currentPixelColor.R;
                    pixels[firstByteNumber + 3] = currentPixelColor.A;
                }
            writeableBitmap.WritePixels(new Int32Rect(0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight), pixels, writeableBitmap.PixelWidth * 4, 0);
        }
    }
}
